//
//  VisitorLoginView.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/24.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit
import FFAutoLayout

/// 定义协议
protocol VisitorLoginViewDelegate: NSObjectProtocol
{
    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
}
class VisitorLoginView: UIView {
    
    /// 定义代理变量
    weak var delegate: VisitorLoginViewDelegate?
    
    // 注册按钮的点击事件调用的方法
    func register() {
        delegate?.visitorRegisterButtonClicked()
    }
    // 登录按钮点击事件调用的方法
    func login() {
        delegate?.visitorLoginButtonClicked()
    }
    
    /// 设置视图内容
    func setupViewInfo(iconName: String, message: String, isHome: Bool) {
        messageLabel.text = message
        // 如果是首页
        if isHome {
            // 首页显示
            homeIconView.hidden = false
            // 显示小房子
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        }else{
            // 不显示首页
            homeIconView.hidden = true
            // 显示传入的图片
            iconView.image = UIImage(named: iconName)
            // 将视图层次置后，防止遮罩错误遮盖
            sendSubviewToBack(maskIconView)
        }
    }
    
    /// 启动动画
    func starAnimation() {
        // 定义一个基本动画
        let anim = CABasicAnimation(keyPath: "transform:rotation")
        // 转一周
        anim.toValue = 2 * M_PI
        // 一周20秒
        anim.duration = 20
        // 一直重复
        anim.repeatCount = MAXFLOAT
        // 添加动画
        iconView.layer.addAnimation(anim, forKey: nil)
        

    }

    // 默认的构造方法，重写这个方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置UI
        setupUI()
    }
    // 必须写啊，要不就报错
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 设置页面上的控件
    func setupUI() {
        // 先添加每个控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置约束
        // iconView水平垂直居中对齐
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
        // 小房子水平居中对齐
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSizeMake(240, 40), offset: CGPoint(x: 0, y: 0))
        // 遮盖view的约束
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0))
        // 登录按钮和注册按钮的约束
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSizeMake(100, 40), offset: CGPoint(x: 0, y: 20))
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSizeMake(100, 40), offset: CGPoint(x: 0, y: 20))
    }
    
    
    ///各种懒加载
    /// 图标
    lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    /// 遮盖的view
    lazy var maskIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()

    /// 小房子
    lazy var homeIconView: UIImageView = {
       let image = UIImage(named: "visitordiscover_feed_image_house")
       return UIImageView(image: image)
    }()
    /// 文字标签
    lazy var messageLabel: UILabel = {
        let label = UILabel(frame: CGRectMake(0, 0, 10, 20))
        // 设置label的各种属性
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        label.sizeToFit()
        return label
    }()
    /// 注册按钮
    lazy var registerButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 140, 40))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 添加点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        }()
    /// 登录按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 220, 140, 40))
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 添加点击事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
